package com.sc.syllabus.work_hours;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sc.syllabus.R;
import com.sc.syllabus.work_hours.bean.WorkHoursBean;
import com.sc.syllabus.work_hours.model.WorkHoursModelImpl;
import com.srain.utils.LogUtils;

import java.util.List;

/**
 * Created by liushuanglong on 2017/9/12.
 * <p>
 * 工时统计
 */

public class WorkHoursActivity extends Activity {

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, WorkHoursActivity.class);
        context.startActivity(intent);
    }

    private static final String TAG = "WorkHoursActivity";
    private ListView listView;
    private TextView textView;
    private WorkHoursModelImpl mModel;
    private HoursAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_hours);
        mModel = new WorkHoursModelImpl(this);
        init();
    }

    private void init() {
        listView = (ListView) findViewById(R.id.listview);
        textView = (TextView) findViewById(R.id.text);
        textView.setOnClickListener(mClickListener);
        listView.setOnItemClickListener(mItemClickListener);
        listView.setOnItemLongClickListener(mLongClickListener);
        List<WorkHoursBean> beans = mModel.queryAll();
        if (null != beans && !beans.isEmpty()) {
            if (null == adapter) {
                adapter = new HoursAdapter(WorkHoursActivity.this, beans);
            }
            listView.setAdapter(adapter);
        } else {
            textView.setText("Beans is empty!!!");
            LogUtils.e(TAG, "beans is empty!!!");
        }
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.text:
                    EditHoursInfoActivity.actionStart(WorkHoursActivity.this);
                    break;
            }
        }
    };

    private AdapterView.OnItemLongClickListener mLongClickListener = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//            WorkHoursBean bean = adapter.getItem(position);
//            int deleteId = mModel.deleteByObject(bean);
//            if (deleteId > 0) {
//                adapter.notifyDataSetInvalidated();
//            }
//            LogUtils.i(TAG, " mLongClickListener --- id == " + deleteId);
            return true;
        }
    };

    private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            WorkHoursBean bean = adapter.getItem(position);
            EditHoursInfoActivity.actionStart(WorkHoursActivity.this, bean);
        }
    };
}
